/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.junit.client.GWTTestCase;

import fr.openparts.OpenParts.client.OPBinding;
import fr.openparts.OpenParts.client.OPSchedulerBinding;
import fr.openparts.OpenParts.client.OPShell;

/**
 * GWT tests for the scheduler of OPBinding.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 07 December 2010
 */
public class OPSharedBindingSchedulerTestGwt extends GWTTestCase {

    private OPBinding binding;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called before each test, set up the binding.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected final void gwtSetUp() throws Exception {
        super.gwtSetUp();
        binding = OPShell.opBindingSharedInstance();
    }

    /**
     * Called at the end of each test to clean up the mess.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtTearDown()
     */
    @Override
    protected final void gwtTearDown() throws Exception {
        super.gwtTearDown();
        unbindOPScheduler(OPShell.opBindingSharedInstance());
    }

    /**
     * Test binding a scheduler. Should get something not null.
     */
    public final void testBindingScheduler() {
        binding.bind(OPSchedulerBinding.schedulerBindingFactory());
        assertNotNull("scheduler shouldn't be null", getScheduler(binding));
    }

    /**
     * Test binding a null scheduler. Should raise an assertion error.
     */
    public final void testBindingNullScheduler() {
        if (assertionsAreEnabled()) {
            try {
                binding.bind((OPSchedulerBinding) null);
                fail("OPBinding should raise an exception when binding a null scheduler.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to schedule a null command. Should raise an assertion error.
     */
    public final void testSchedulingNullCommand() {
        if (assertionsAreEnabled()) {
            try {
                binding.scheduleCommand(null);
                fail("OPBinding should raise an exception when scheduling a null command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Returns the scheduler of the binding.
     * 
     * @param binding
     *            the binding
     * @return the scheduler
     */
    private final native OPSchedulerBinding getScheduler(OPBinding binding) /*-{
        return binding.scheduler;
    }-*/;

    /**
     * Unbind the OPScheduler.
     */
    private final native void unbindOPScheduler(final OPBinding binding) /*-{
        binding.scheduler = null;
    }-*/;

    /**
     * Check if assertions are enabled.
     * 
     * @return true if assertions are enabled, false otherwise
     */
    private final boolean assertionsAreEnabled() {
        try {
            assert false;
            return false;
        } catch (AssertionError e) {
            return true;
        }
    }
}
